<?php
namespace yan\widgets;
use Yii;
use yii\helpers\Html;
/**
 * <?=\yan\widgets\RadioInput::widget([
                'model'=>$model,
                'attribute' => 'is_new',
                'options' => [
                    'items'=>[
                        '0' => Yii::t('app', 'No'),
                        '1' => Yii::t('app', 'Yes'),
                    ],
                ],
                'targetOptions'=>[
                    'triggerValue'=>1,
                    'targetAttribute'=>'sales_memo',
                    'inputOptions'=>[
                        'class'=>'layui-input',
                        'placeholder'=>Yii::t('app', 'eg.: 8170/1201234')
                    ],
                ],
            ])?>
 * */
class RadioInput extends \yii\widgets\InputWidget{
	public $targetOptions;
	public $targetName;
	public $targetValue;
	public $triggerValue;
	public function init(){
        parent::init();
        if ($this->hasModel()) {
            $this->name = Html::getInputName($this->model, $this->attribute);
            $this->value = Html::getAttributeValue($this->model, $this->attribute);  
            
            if (!array_key_exists('id', $this->options)) {
                $this->options['id'] = Html::getInputId($model, $attribute);
            }
            if(isset($this->targetOptions['targetAttribute'])){
            	$this->targetName = Html::getInputName($this->model, $this->targetOptions['targetAttribute']);
            	$this->targetValue = Html::getAttributeValue($this->model, $this->targetOptions['targetAttribute']);
            	if (!array_key_exists('id', $this->targetOptions)) {
	                $this->targetOptions['id'] = Html::getInputId($this->model, $this->targetOptions['targetAttribute']);
	            }
            }
        }
        if(isset($this->targetOptions['triggerValue'])){
    		$this->triggerValue = $this->targetOptions['triggerValue'];
    	}
        if( !isset($this->options['class']) ){
            $this->options['class'] = 'flag';
        }else if( strpos($this->options['class'], 'flag') === false){
            $this->options['class'] .= ' flag';
        }
        
        $this->registerJs();
    }

    public function run(){
        return $this->renderInputHtml('checkbox');
    }

    protected function renderInputHtml($type)
    {
    	$items = [];
    	if(isset($this->options['items'])){
    		$items = $this->options['items'];
    		unset($this->options['items']);
    	}
    	$inputOptions = [];
    	if(isset($this->options['inputOptions'])){
    		$inputOptions = $this->options['inputOptions'];
    		unset($this->options['inputOptions']);
    	}
        $shtml = Html::beginTag('div', $this->options);
        $shtml .= Html::radioList($this->name, $this->value, $items, $inputOptions);
        if(!empty($this->targetOptions)){
        	$targetOptions = $this->targetOptions;
        	$targetInputOptions = [];
	    	if(isset($targetOptions['inputOptions'])){
	    		$targetInputOptions = $targetOptions['inputOptions'];
	    		unset($targetOptions['inputOptions']);
	    	}
	    	if($this->value != $this->triggerValue){
	    		$targetOptions['style'] = 'display:none;';
	    	}
        	$shtml .= Html::beginTag('div', $targetOptions);
        	$shtml .= Html::textInput($this->targetName, $this->targetValue, $targetInputOptions);
        	$shtml .= Html::endTag('div');
        }
        
        $shtml .= Html::endTag('div');
        return $shtml;
    }

    protected function registerJs(){
        $this->view->registerJs('$(document).on("change", "[name=\"'.$this->name.'\"]", function(){
        	var value = $("[name=\"'.$this->name.'\"]:checked").val();
        	console.log(value);
        	if(value=='.$this->triggerValue.'){
        		$("#'.$this->targetOptions['id'].'").show();
        	}else{
        		$("#'.$this->targetOptions['id'].'").hide();
        	}
        })', \yii\web\View::POS_END);
    }
}